package com.kotle.playermodule

import android.content.Context
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import io.gitee.kotle.player.core.IPlayer
import io.gitee.kotle.player.core.assetResToPath
import io.gitee.kotle.player.core.impl.exoplayer.IExoPlayerMode
import io.gitee.kotle.player.session.IPlayerSession
import io.gitee.kotle.player.session.impl.exoplayer.ExoPlayerMediaBrowser
import kotlinx.coroutines.CoroutineScope

/**
 *
 * 描述：
 *
 * @author Jiale.Wei
 *
 * 创建时间：2023/5/6 12:37
 *
 */
object MediaBrowser {
    private var browser: ExoPlayerMediaBrowser<IExoPlayerMode, MediaService>? = null
    val eventLiveData
        get() = browser?.eventLiveData
    fun connect(context: Context) {
        if (browser == null) {
            browser = object :
                ExoPlayerMediaBrowser<IExoPlayerMode, MediaService>(
                    context.applicationContext,
                    MediaService::class.java
                ) {
                override fun onConnected() {
                    super.onConnected()
                    sendEvent(
                        IPlayerSession.Event(
                            models = mutableListOf(object : IExoPlayerMode {
                                override suspend fun getMediaId(
                                    scope: CoroutineScope,
                                    player: IPlayer<MediaItem, *>
                                ): String {
                                    return assetResToPath("video.mp4")
                                }

                                override suspend fun getMedia(
                                    scope: CoroutineScope,
                                    player: IPlayer<MediaItem, *>,
                                    mediaId: String
                                ): MediaItem {
                                    return MediaItem.Builder()
                                        .setMediaId(mediaId)
                                        .setMediaMetadata(
                                            MediaMetadata.Builder()
                                                .setTitle("title-2")
                                                .setArtist("artist")
                                                .build()
                                        )
                                        .setUri(mediaId)
                                        .build()
                                }

                            }),
                            prepareIndex = 0,
                            play = true,
                        )
                    )
                }
            }
        }
    }
}

